package cn.lijian.goods.user.dao;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import cn.itcast.jdbc.TxQueryRunner;
import cn.lijian.goods.user.domain.User;



/**
 * �û�ģ��  
 * @author lijian
 *
 */
public class UserDao {
	private QueryRunner qr=new TxQueryRunner();
	/**
	 * 按 Uid 和 password 进行查询
	 * @param uid
	 * @param pass
	 * @return
	 * @author lijian
	 * @throws SQLException 
	 */
	public boolean findByUidAndPass(String uid,String pass) throws SQLException{
		String sql="select count(*) from t_user where uid=? and loginpass = ?";
		Number number=(Number)qr.query(sql, new ScalarHandler(),uid,pass);
		return number.intValue()>0;
	}
	/**
	 * 修改密码
	 * @param uid
	 * @param password
	 * @author lijian
	 * @throws SQLException 
	 */
	public void updatePassword(String uid,String password) throws SQLException{
		String sql="update t_user set loginpass=? where uid=?";
		qr.update(sql,password,uid);
	}
	
	/**
	 * 校验用户名是否注册
	 * @param loginname
	 * @return
	 * @author lijian
	 * @throws SQLException 
	 */
	public boolean ajaxValidateLoginname(String loginname) throws SQLException{
		String sql="select count(*) from t_user where loginname=?";
		Number number=(Number)qr.query(sql, new ScalarHandler(),loginname);
		return number.intValue()==0;
	}
	
	/**
	 * email 是否注册
	 * @param loginname
	 * @return
	 * @author lijian
	 * @throws SQLException 
	 */
	public boolean ajaxValidateEmail(String email) throws SQLException{
		String sql="select count(*) from t_user where email=?";
		Number number=(Number)qr.query(sql, new ScalarHandler(),email);
		return number.intValue()==0;
	}
	/**
	 * 添加用户
	 * @param user
	 * @author lijian
	 * @throws SQLException 
	 */
	public void add(User user) throws SQLException{
		String sql="insert into t_user values(?,?,?,?,?,?)";
		Object[]params={user.getUid(),user.getLoginname(),user.getLoginpass(),user.getEmail(),user.isStatus(),user.getActivationCode()};
		qr.update(sql,params);
	}
	/**
	 * 通过激活码查询用户
	 * @param code
	 * @return
	 * @author lijian
	 * @throws SQLException 
	 */
	public User findByCode(String code) throws SQLException{
		String sql="select * from t_user where activationCode=?";
		return qr.query(sql, new BeanHandler<User>(User.class),code);
	}
	/**
	 * 修改用户状态
	 * @param uid
	 * @param status
	 * @author lijian
	 * @throws SQLException 
	 */
	public void updateStatus(String uid,boolean status) throws SQLException{
		String sql="update t_user set status=? where uid=?";
		qr.update(sql,status,uid);
	}
	/**
	 * 按用户名和密码查询
	 * @param loginname
	 * @param loginpass
	 * @return
	 * @author lijian
	 * @throws SQLException 
	 */
	public User findByLoginnameAndLoginpass(String loginname,String loginpass) throws SQLException{
		String sql="select * from t_user where loginname=? and loginpass=?";
		return qr.query(sql, new BeanHandler<User>(User.class),loginname,loginpass);
	}
	
}
